{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yeadDkMiISin"
      },
      "source": [
        "# Gemini API: Authentication Quickstart"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lEXQ3OwKIa-O"
      },
      "source": [
        "<table align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "df1767a3d1cc"
      },
      "source": [
        "The Gemini API uses API keys for authentication. This notebook walks you through creating an API key, and using it with the Python SDK or a command line tool like `curl`."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mhFKmRmxi5B-"
      },
      "source": [
        "## Create an API key\n",
        "\n",
        "You can [create](https://aistudio.google.com/app/apikey) your API key using Google AI Studio with a single click.  \n",
        "\n",
        "Remember to treat your API key like a password. Do not accidentally save it in a notebook or source file you later commit to GitHub. This notebook shows you two ways you can securely store your API key.\n",
        "\n",
        "* If you are using Google Colab, we recommend you store your key in Colab Secrets.\n",
        "\n",
        "* If you are using a different development environment (or calling the Gemini API through `cURL` in your terminal), we recommend you store your key in an environment variable.\n",
        "\n",
        "Let's start with Colab Secrets."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dEoigYI9Jw_K"
      },
      "source": [
        "## Add your key to Colab Secrets\n",
        "\n",
        "Add your API key to the Colab Secrets manager to securely store it.\n",
        "\n",
        "1. Open your Google Colab notebook and click on the 🔑 **Secrets** tab in the left panel.\n",
        "   \n",
        "   <img src=\"https://storage.googleapis.com/generativeai-downloads/images/secrets.jpg\" alt=\"The Secrets tab is found on the left panel.\" width=50%>\n",
        "\n",
        "2. Create a new secret with the name `GOOGLE_API_KEY`.\n",
        "3. Copy/paste your API key into the `Value` input box of `GOOGLE_API_KEY`.\n",
        "4. Toggle the button on the left to allow notebook access to the secret.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jRY1eioF4gUB"
      },
      "source": [
        "## Install the Python SDK"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xuiLSV7amy3P"
      },
      "outputs": [],
      "source": [
        "!pip install -U -q google-generativeai"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3dw8ygh74mVc"
      },
      "source": [
        "## Configure the SDK with your API key\n",
        "\n",
        "You'll call `genai.configure` with your API key, but instead of pasting your key into the notebook, you'll read it from Colab Secrets."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "DTl-qZp34sht"
      },
      "outputs": [],
      "source": [
        "import google.generativeai as genai\n",
        "from google.colab import userdata\n",
        "\n",
        "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
        "genai.configure(api_key=GOOGLE_API_KEY)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tr7oAO6-nMsE"
      },
      "source": [
        "And that's it! Now you're ready to call the Gemini API."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "n6sXnWrJoKoo"
      },
      "outputs": [],
      "source": [
        "model = genai.GenerativeModel('gemini-1.0-pro')\n",
        "response = model.generate_content(\"Please give me python code to sort a list.\")\n",
        "print(response.text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BTdQtZri1Brs"
      },
      "source": [
        "## Store your key in an environment variable"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gZDX51Y27pN4"
      },
      "source": [
        "If you are using a different development environment (or calling the Gemini API through `cURL` in your terminal), we recommend you store your key in an environment variable.\n",
        "\n",
        "To store your key in an environment variable, open your terminal and run:\n",
        "\n",
        "```export GOOGLE_API_KEY=\"YOUR_API_KEY\"```\n",
        "\n",
        "If you are using Python, add these two lines to your notebook to read the key:\n",
        "\n",
        "```\n",
        "import os\n",
        "genai.configure(api_key=os.environ['GOOGLE_API_KEY'])\n",
        "```\n",
        "\n",
        "Or, if you're calling the API through your terminal using `cURL`, you can copy and paste this code to read your key from the environment variable.\n",
        "\n",
        "```\n",
        "curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$API_KEY \\\n",
        "    -H 'Content-Type: application/json' \\\n",
        "    -X POST \\\n",
        "    -d '{\n",
        "      \"contents\": [{\n",
        "        \"parts\":[{\n",
        "          \"text\": \"Please give me Python code to sort a list.\"}]}]}'\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CAOKOcax1xZY"
      },
      "source": [
        "## Learning more\n",
        "\n",
        "The Gemini API uses API keys for most types of authentication, and that’s all you need to get started. We use OAuth for more advanced authentication when tuning models. You can learn more about that in the [OAuth quickstart](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication_with_OAuth.ipynb)."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "Authentication.ipynb",
      "toc_visible": true
    },
    "google": {
      "image_path": "/site-assets/images/share.png",
      "keywords": [
        "examples",
        "googleai",
        "samplecode",
        "python",
        "embed",
        "function"
      ]
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
